package com.sanbi.erp.db.util.easyExcel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.sanbi.erp.db.domain.ProcurementPlanEasyExcelAdd;
import com.sanbi.erp.db.domain.ProductEasyExcelAdd;
import com.sanbi.erp.db.domain.ProductEasyExcelGroupAdd;
import com.sanbi.erp.db.domain.ProductEasyExcelUpdate;
import com.sanbi.erp.db.util.CheckoutUtil;
import lombok.extern.slf4j.Slf4j;
import java.util.List;


/**
 * @introduce: Excel读取监听器
 * @Date: 2022/10/20 11:34
 * @Author: XYP
 */
@Slf4j
public class EasyExcelListener extends AnalysisEventListener<Object> {

    EasyExcelService easyExcelService = new EasyExcelService();

    private static final int BATCH_COUNT = 100;

    // 存储导入到数据库中的数据
    private List<Object> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);

    /**
     * 读取Excel时，它是一条一条读取的
     * 每读取一条数据它就会调用此方法一次
     * @param o 读取对象的类型
     * @param analysisContext
     */
    @Override
    public void invoke(Object o, AnalysisContext analysisContext) {
        // 添加到集合
        cachedDataList.add(o);
        // 每100条数据
        // if (cachedDataList.size() > BATCH_COUNT) {
            // saveData();
            // cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        // }
    }

    /**
     * 读取完所有数据后，调用此方法
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (cachedDataList.get(CheckoutUtil.TYPE_ZERO) instanceof ProductEasyExcelUpdate) {
            updateData();
            log.info("{数据批量更新成功update-OK}");
        } else if (cachedDataList.get(CheckoutUtil.TYPE_ZERO) instanceof ProductEasyExcelAdd) {
            saveData();
            log.info("{数据批量添加成功insert-OK}");
        } else if (cachedDataList.get(CheckoutUtil.TYPE_ZERO) instanceof ProductEasyExcelGroupAdd) {
            saveGroupData();
            log.info("{数据批量添加成功insertGroup-OK}");
        } else if (cachedDataList.get(CheckoutUtil.TYPE_ZERO) instanceof ProcurementPlanEasyExcelAdd) {
            saveDataProcurementPlan();
            log.info("{数据批量添加成功insertProcurementPlan-OK}");
        }

         // log.info(cachedDataList.toString());
        cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    }

    /**
     * 往数据库添加数据（单个商品）
     */
    private void saveData() {
        log.info("读取到{}条数据，开始批量导入到数据库中...", cachedDataList.size());
        this.easyExcelService.insertSelectiveList((List<ProductEasyExcelAdd>)(List)cachedDataList);
    }

    /**
     * 批量修改（单个商品）
     */
    private void updateData() {
        log.info("读取到{}条数据，开始批量修改...", cachedDataList.size());
        this.easyExcelService.updateData((List<ProductEasyExcelUpdate>)(List)cachedDataList);
    }

    /**
     * 往数据库添加数据（组合商品）
     */
    private void saveGroupData() {
        log.info("读取到{}条数据，开始批量导入到数据库中...", cachedDataList.size());
        this.easyExcelService.insertSelectiveGroupList((List<ProductEasyExcelGroupAdd>)(List)cachedDataList);
    }

    /**
     * 采购计划批量导入
     */
    private void saveDataProcurementPlan() {
        log.info("读取到{}条数据，开始批量导入到数据库中...", cachedDataList.size());
        this.easyExcelService.insertSelectiveProcurementPlanList((List<ProcurementPlanEasyExcelAdd>)(List)cachedDataList);
    }
}
